package com.linxz.wandnroid.appui;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.core.view.GravityCompat;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.viewpager.widget.ViewPager;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.linxz.evens.ARouterURLConfig;
import com.linxz.framework.base.AppActivityManager;
import com.linxz.framework.base.BaseActivity;
import com.linxz.ui.appbar.StatusBarUtil;
import com.linxz.ui.pager_indictor.ScaleTransitionPagerTitleView;
import com.linxz.wandnroid.R;
import com.linxz.wandnroid.adapter.HomePagerAdapter;
import com.linxz.wandnroid.enums.CHANNEL;

import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.ViewPagerHelper;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.SimplePagerTitleView;

/**
 * @author Linxz
 * 创建日期：2020年02月20日 23:02
 * version：v4.5.4
 * 描述：
 */
@Route(path = ARouterURLConfig.WAN_ANDROID_HOME)
public class WanAndroidHomeActivity extends BaseActivity {

    private DrawerLayout drawerLayout;
    private ViewPager mViewPager;
    private CHANNEL[] CHANNELS=new CHANNEL[]{CHANNEL.ARTICLE,CHANNEL.PROJECTS,CHANNEL.SYSTEM};

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        setTheme(R.style.DrawerLayout_APPTheme);
        StatusBarUtil.statusBarLightMode(this);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.wandroid_act_home);
        initUI();
        initData();
    }

    @Override
    protected void initUI() {
        drawerLayout=findViewById(R.id.drawerLayout);
        mViewPager=findViewById(R.id.view_pager);
        HomePagerAdapter adapter = new HomePagerAdapter(getSupportFragmentManager(), CHANNELS);
        mViewPager.setAdapter(adapter);
        initMagicIndicator();
        mViewPager.setCurrentItem(1,false);
    }

    @Override
    protected void initData() {
    }

    private void initMagicIndicator() {
        MagicIndicator magicIndicator = findViewById(R.id.magic_indicator);
        magicIndicator.setBackgroundColor(Color.WHITE);
        CommonNavigator commonNavigator = new CommonNavigator(this);
        commonNavigator.setAdjustMode(true);
        commonNavigator.setAdapter(new CommonNavigatorAdapter() {
            @Override
            public int getCount() {
                return CHANNELS == null ? 0 : CHANNELS.length;
            }

            @Override
            public IPagerTitleView getTitleView(Context context, final int index) {
                SimplePagerTitleView simplePagerTitleView = new ScaleTransitionPagerTitleView(context);
                simplePagerTitleView.setText(CHANNELS[index].getName());
                simplePagerTitleView.setTextSize(19);
                simplePagerTitleView.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
                simplePagerTitleView.setNormalColor(Color.parseColor("#999999"));
                simplePagerTitleView.setSelectedColor(Color.parseColor("#333333"));
                simplePagerTitleView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mViewPager.setCurrentItem(index);
                    }
                });
                return simplePagerTitleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
                return null;
            }

            @Override
            public float getTitleWeight(Context context, int index) {
                return 1.0f;
            }
        });
        magicIndicator.setNavigator(commonNavigator);
        ViewPagerHelper.bind(magicIndicator, mViewPager);
    }


    public void onLeftOpen(View view){
       if (drawerLayout.isDrawerOpen(GravityCompat.START)){
           drawerLayout.closeDrawers();
       }else{
           drawerLayout.openDrawer(GravityCompat.START);
       }
    }

    public void onSearch(View view){
        AppActivityManager.getInstance().goTo(ARouterURLConfig.SEARCH_ACT);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
